<template>
  <el-dialog
    title="编辑员工"
    :visible="showDialog"
    width="40%"
    @close="closeFn"
  >
    <el-form ref="form" :rules="rules" :model="formDate" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formDate.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formDate.email" placeholder="请输入邮箱" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="editFn">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getUserInfoById, assigRoles } from '@/api/yuangong'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formDate: {
        username: '',
        email: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在3到5个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // // 获取单个员工详情
    async getUserInfoById(id) {
      const res = await getUserInfoById(id)
      //   console.log(87, res)
      this.formDate = res.data
    },
    // 取消按钮
    editFn() {
      this.closeFn()
    },
    // 确认编辑
    async submit() {
      // 表单校验
      this.$refs.form.validate()
      //   添加员工
      await assigRoles(this.formDate)
      //   // 关闭弹窗
      this.closeFn()
      // 更新页面页码数量
      this.$emit('updateedit')
    },
    // 封装退出功能
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空校验规则
      this.$refs.form.resetFields()
      //   清空数据
      this.formDate = {
        username: '',
        email: '',
        password: ''
      }
    }
  }
}
</script>

<style>

</style>
